<script setup lang="ts">
// 定义组件名
defineOptions({
    name: 'LoginPage'
})
import { loginByCodeAPI, loginByPasswordAPI, sendMSMCode } from '@/apis/user';
import { RulesCodes, RulesMobile, RulesPassword } from '@/utils/rules';
import { type FormInstance, showFailToast, showToast } from 'vant';
import { ref } from 'vue';
import { useUserStore } from '@/stores'
import router from '@/router'


//定义响应式数据
const mobile = ref('')
const password = ref('')
//是否勾选
const isAgree = ref(false)

//表单提交时
const onSubmit = async (val: object) => {
    //如果用户没有勾选我同意  代码要停止执行
    if (!isAgree.value) return showFailToast('请勾选我同意')
    const res = isPass.value ? await loginByPasswordAPI(val) : await loginByCodeAPI(mobile.value, code.value)
    showToast('登录成功')
    const store = useUserStore()
    store.setUser(res.data)
    router.push('/user')

}
const formRef = ref<FormInstance>()
//发送短信验证码
const time = ref(0)
let timerId: number
const onSend = async () => {
    //处理是否已经在发送短信验证码
    if (time.value > 0) return
    //校验是否输入了收集号码
    await formRef.value?.validate('mobile')
    //发送短信验证码
    const res = await sendMSMCode(mobile.value, 'login')
    //显示验证码
    showToast({
        message: res.data.code,
        duration: 5000
    })
    //处理倒计时
    time.value = 60
    if (timerId) clearInterval(timerId)
    //开启定时器函数
    timerId = setInterval(() => {
        time.value--
        if (time.value <= 0) {
            clearInterval(timerId)
        }
    }, 1000)
}

// onMounted(() => {
//   QC.Login({
//     btnId: 'qq'
//   })
// })

// onUnmounted(() => {
//     //清除定时器
//     clearInterval(timerId)
// })

//切换 密码和短信验证码
const onChangePass = () => {
    isPass.value = !isPass.value
    //重置表单项的验证提示
    formRef.value?.resetValidation()
}


const isPass = ref(true)
const code = ref('')
const isShow = ref(false)
import cpIcon from '@/components/cpIcon.vue';
</script>

<template>
    <div class="login-page">
        <cp-nav-bar right-text="注册" @click-right="router.push('/register')"></cp-nav-bar>
        <!-- 头部 -->
        <div class="login-head">
            <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
            <a href="javascript:;" @click="onChangePass">
                <span>{{ !isPass ? '密码登录' : '短信验证码登录' }}</span>
                <van-icon name="arrow"></van-icon>
            </a>
        </div>
        <!-- 表单 -->
        <van-form autocomplete="off" @submit="onSubmit" ref="formRef">
            <van-field placeholder="请输入手机号" type="tel" name="mobile" v-model="mobile" :rules="RulesMobile"></van-field>
            <van-field v-if="isPass" placeholder="请输入密码" :type="isShow ? 'text' : 'password'" name="password"
                v-model="password" :rules="RulesPassword">
                <template #button>
                    <cp-icon @click="isShow = !isShow" :name="`${isShow ? 'login-eye-on' : 'login-eye-off'}`"
                        class="icon-eye"></cp-icon>
                </template>
            </van-field>
            <van-field v-else v-model="code" placeholder="请输入验证码" :rules="RulesCodes">
                <template #button>
                    <span class="btn-send" @click="onSend" :class="{ active: time > 0 }">{{ time > 0 ? `${time}秒后发送验证码`
                        : '发送验证码'
                        }}</span>
                </template>
            </van-field>
            <div class="cp-cell">
                <van-checkbox v-model="isAgree">
                    <span>我已同意</span>
                    <a href="javascript:;">用户协议</a>
                    <span>及</span>
                    <a href="javascript:;">隐私条款</a>
                </van-checkbox>
            </div>
            <div class="cp-cell">
                <van-button block round type="primary" native-type="submit">登 录</van-button>
            </div>
            <div class="cp-cell">
                <a href="javascript:;">忘记密码？</a>
            </div>
        </van-form>
        <!-- 底部 -->
        <div class="login-other">
            <van-divider>第三方登录</van-divider>

            <a class="icon"
                href="https://graph.qq.com/oauth2.0/show?which=Login&display=pc&client_id=102015968&response_type=token&scope=all&redirect_uri=http%3A%2F%2Fconsult-patients.itheima.net%2Flogin%2Fcallback">
                <img src="@/assets/qq.svg" alt="" />
            </a>

        </div>
    </div>

</template>

<style lang="scss" scoped>
@import '@/styles/login.scss'
</style>